<?php
/*
 * 车系显示用到的部分函数归类
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: a38f47bb71e1f2bac5ca8b908c268d5f9dec747c $
 */
defined('IN_IXDCW') or exit('Access Denied');
/**
 * Class Auto
 */
class Auto {

	public static $typenames = array(
		'canshu'   => '参数配置',
		'main'     => '综述',
		'video'    => '视频',
		'maintain' => '保养',
		'news'     => '资讯',
		'guide'    => '导购',
		'quote'    => '行情',
		'owners'   => '车主圈',
		'pic'      => '图片',
		'comment'      => '评论',
	);

	public static function catsort($arr, $pid = 0, $lev = 0) {
		static $list = array();
		if ($arr) {
			foreach ($arr as $v) {
				if ($v['parentid'] == $pid) {
					$v['lev'] = $lev;
					$list[]   = $v;
					self::catsort($arr, $v['arid'], $lev + 2);
				}
			}
		}
		return $list;
	}

	/**
	 * 从缓存中读取品牌列表， 不涉及到数据库查询
	 * @param $RANK
	 * @return array
	 */
	public static function brands($RANK) {
		$BRAND = array();
		foreach ($RANK as $id => $rank) {
			// get first level
			if ($rank['parentid'] == 0) {
				$rank['letter'] = strtoupper($rank['letter']);
				$BRAND[$id]     = $rank;
			}
		}
		return $BRAND;
	}


	/**
	 * 获取存在的字母
	 * @param $BRAND
	 * @return array
	 */
	public static function uniqueLetter($BRAND) {
		$letters = array();
		foreach ($BRAND as $brand) {
			if (isset($letters[$brand['letter']])) {
				continue;
			} else {
				$letters[$brand['letter']] = '';
			}
		}
		return array_keys($letters);
	}

	/**
	 * 格式化价格
	 * @param $price
	 * @return string
	 */
	public static function formatPrice($price) {
		return round($price / 10000, 2) . '万';
	}

	/**
	 * 价格区间
	 * @param $models
	 * @return string
	 */
	public static function priceRange($models) {
		$noPrice = '暂无报价';
		if (count($models) == 1) {
			if ($models[0]['price'] == 0) {
				return $noPrice;
			}
			return Auto::formatPrice($models[0]['price']);
		} else {
			$prices = array_keys(__($models)->groupBy('price'));
			$max    = max($prices);
			$min    = min($prices);
			if ($max == 0) {
				return $noPrice;
			}
			return Auto::formatPrice($min) . '~' . Auto::formatPrice($max);
		}
	}

	/**
	 * 级别范围
	 * @param $models
	 * @return string
	 */
	public static function levelRange($models) {
		$tmp = (array)__(array_keys(__($models)->groupBy('acid')))->map(function ($levelid) {
			return AutoCategory::getCacheItem(AutoCategory::TYPE_LEVEL, $levelid);
		});
		return implode(',', $tmp);
	}


	public static function gearboxRange($models) {
		$tmp = (array)__(array_keys(__($models)->groupBy('gearbox')))->map(function ($levelid) {
			return AutoCategory::getCacheItem(AutoCategory::TYPE_GEARBOX, $levelid);
		});
		return implode(',', $tmp);
	}

	/**
	 * 返回类型名称
	 * @param $type
	 * @return string
	 */
	public static function typename($type) {
		if (isset(self::$typenames[$type])) {
			return self::$typenames[$type];
		} else {
			return '';
		}
	}

	/**
	 * 获取name
	 * @param $arid
	 * @return null
	 */
	public static function getName($arid) {
		global $db;
		$table = Table::name(TABLE::NAME_AUTO_CATEGORY);
		$sql = "select name from $table where arid = $arid";
		return $db->getVar($sql);
	}

	/**
	 * 获取多个车款名称
	 * @param $carid
	 * @return array
	 */
	public static function getManyModelName($carid) {
		global $db;
		$table = Table::name(TABLE::NAME_AUTO_MODEL);
		$sql = "select amid,name from $table where arcid=$carid and issale=1";
		return $db->getAll($sql);
	}

	public static function decide404($arid) {
		global $db;
		$table = Table::name(TABLE::NAME_AUTO_CATEGORY);
		$sql = "select * from $table where arid = $arid";
		if (!$db->getVar($sql, 'arid')){
			Prompt::error404();
		}
	}


}